﻿using duqin.Common;
using duqin.Entities;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace duqin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly ILogger<UsersController> _logger;
        private readonly IFreeSql _freeSql;

        public UsersController(ILogger<UsersController> logger, IFreeSql freeSql)
        {
            _logger = logger;
            _freeSql = freeSql;
        }

        // GET: api/<UsersController>
        [HttpGet]
        public async Task<IActionResult> Get(int page = 1, int limit = 10, string? type = "")
        {
            var query = _freeSql.Select<User>().WhereIf(!string.IsNullOrEmpty(type), x => x.Type == type);
            return ServerResponse.Page(await query.ToPageAsync(page, limit));
        }

        // GET api/<UsersController>/5
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var query = _freeSql.Select<User>(id);
            return ServerResponse.Yes(await query.ToOneAsync());
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // POST api/<UsersController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // PUT api/<UsersController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // DELETE api/<UsersController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
